package design.mode.effective_java.chapter7.item45;

import java.math.BigInteger;
import java.util.stream.LongStream;

/**
 * @author ZhongMingde on 2021/8/31
 */

public class Test2 {
    public static void main(String[] args) {
        // "Hello world !".chars().forEach(x -> System.out.println((char) x));
        long start = System.nanoTime();
        pi(10000);
        long end = System.nanoTime();
        System.out.println((double) (end - start) / 1_000_000_000);
        start = System.nanoTime();
        pi(10000);
        end = System.nanoTime();
        System.out.println((double) (end - start) / 1_000_000_000);
    }

    static long pi(long n) {
        return LongStream.rangeClosed(2, n)
                .mapToObj(BigInteger::valueOf)
                .filter(i -> i.isProbablePrime(50))
                .count();
    }

    static long pi2(long n) {
        return LongStream.rangeClosed(2, n)
                .parallel()
                .mapToObj(BigInteger::valueOf)
                .filter(i -> i.isProbablePrime(50))
                .count();
    }


}
